package com.diploma.project.aiqiyi.utils;

public class SqlParamUtil {
    /** mysql 特殊字符 */
    private static final String[] _SPECIAL_STR = { "%", "_" };
    private static final String begin_time = " 00:00:00";
    private static final String end_time = " 23:59:59";

    /**
     * 检查入参是否为空字符串，如果为空字符串，则返回null
     */
    public static <T> T getVal(T str) {
        if (str == null || "".equals(str))
            return null;
        return str;
    }

    /**
     * 匹配左侧任意类型和长度的字符
     */
    public static String getLeftLike(String str) {
        if (getVal(str) != null)
            return "%" + replaceStr(str);
        return null;
    }

    /**
     * 匹配右侧任意类型和长度的字符
     */
    public static String getRightLike(String str) {
        if (getVal(str) != null)
            return replaceStr(str) + "%";
        return null;
    }

    /**
     * 匹配两侧任意类型和长度的字符
     */
    public static String getBetweenLike(String str) {
        if (getVal(str) != null)
            return "%" + replaceStr(str) + "%";
        return null;
    }

    /**
     * 替换sql中特殊字符
     */
    public static String replaceStr(String str) {
        for (int i = 0; i < _SPECIAL_STR.length; i++) {
            if (str.contains(_SPECIAL_STR[i])) {
                str = str.replaceAll(_SPECIAL_STR[i], "\\\\" + _SPECIAL_STR[i]);
            }
        }
        return str;
    }

    public static String[] appendTime(String[] dateArr) {
        if (dateArr == null || dateArr.length == 0)
            return dateArr;

        String[] arr = new String[dateArr.length];
        if (StringUtil.isNotBlank(dateArr[0])) {
            arr[0] = dateArr[0].concat(begin_time);
        }
        if (StringUtil.isNotBlank(dateArr[1])) {
            arr[1] = dateArr[1].concat(end_time);
        }
        return arr;
    }

    public static String appendBeginTime(String beginDate) {
        if (StringUtil.isBlank(beginDate))
            return null;
        return beginDate.concat(begin_time);
    }

    public static String appendEndTime(String endDate) {
        if (StringUtil.isBlank(endDate))
            return null;
        return endDate.concat(end_time);
    }

}
